package org.example.system.controller;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.system.common.model.Option;
import org.example.system.common.result.Result;
import org.example.system.model.form.DeptForm;
import org.example.system.model.query.DeptQuery;
import org.example.system.model.vo.DeptVO;
import org.example.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import org.example.system.service.SysDeptService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 * 处理部门相关的请求，包括获取列表、添加、更新和删除部门
 *
 * @author LY
 */
@Tag(name = "05.部门接口")
@RestController
@RequestMapping("/api/v1/dept")
@RequiredArgsConstructor
public class SysDeptController {

    // 注入部门服务
    private final SysDeptService deptService;

    // 权限常量
    private static final String PERM_ADD = "sys:dept:add";
    private static final String PERM_EDIT = "sys:dept:edit";
    private static final String PERM_DELETE = "sys:dept:delete";

    /**
     * 获取部门列表
     *
     * @param queryParams 查询参数
     * @return 部门列表
     */
    @Operation(summary = "获取部门列表")
    @GetMapping
    public Result<List<DeptVO>> listDepartments(@ParameterObject DeptQuery queryParams) {
        List<DeptVO> list = deptService.listDepartments(queryParams);
        return Result.success(list);
    }

    /**
     * 获取部门下拉选项
     *
     * @return 部门下拉选项列表
     */
    @Operation(summary = "获取部门下拉选项")
    @GetMapping("/options")
    public Result<List<Option>> listDeptOptions() {
        List<Option> list = deptService.listDeptOptions();
        return Result.success(list);
    }

    /**
     * 获取部门表单数据
     *
     * @param deptId 部门ID
     * @return 部门表单数据
     */
    @Operation(summary = "获取部门表单数据")
    @GetMapping("/{deptId}/form")
    public Result<DeptForm> getDeptForm(@Parameter(description = "部门ID") @PathVariable Long deptId) {
        DeptForm deptForm = deptService.getDeptForm(deptId);
        return Result.success(deptForm);
    }

    /**
     * 新增部门
     *
     * @param formData 部门表单数据
     * @return 新增部门的ID
     */
    @Operation(summary = "新增部门")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('" + PERM_ADD + "')")
    @PreventDuplicateSubmit
    public Result<Long> saveDept(@Valid @RequestBody DeptForm formData) {
        Long id = deptService.saveDept(formData);
        return Result.success(id);
    }

    /**
     * 修改部门
     *
     * @param deptId 部门ID
     * @param formData 部门表单数据
     * @return 更新后的部门ID
     */
    @Operation(summary = "修改部门")
    @PutMapping("/{deptId}")
    @PreAuthorize("@ss.hasPerm('" + PERM_EDIT + "')")
    public Result<Long> updateDept(@PathVariable Long deptId, @Valid @RequestBody DeptForm formData) {
        deptId = deptService.updateDept(deptId, formData);
        return Result.success(deptId);
    }

    /**
     * 删除部门
     *
     * @param ids 部门ID，多个以英文逗号(,)分割
     * @return 删除结果
     */
    @Operation(summary = "删除部门")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('" + PERM_DELETE + "')")
    public Result<Void> deleteDepartments(@Parameter(description = "部门ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids) {
        boolean result = deptService.deleteByIds(ids);
        return Result.judge(result);
    }
}